package com.example.yichuangyun.dialog;

import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.yichuangyun.R;
import com.example.yichuangyun.utils.DensityUtil;

public abstract class ContactWayPopWindow extends PopupWindow {
    private final PopupWindow popupWindow;

    public ContactWayPopWindow(Context context) {
        View contentView = LayoutInflater.from(context).inflate(R.layout.contact_way_pop_layout, null);
        popupWindow = new PopupWindow(contentView,
                DensityUtil.dip2px(context, 100), ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);
        TextView tvPhone = contentView.findViewById(R.id.tv_phone);
        TextView tvQq = contentView.findViewById(R.id.tv_qq);
        TextView tvWeChat = contentView.findViewById(R.id.tv_we_chat);
        TextView tvEmail = contentView.findViewById(R.id.tv_email);
        TextView tvLandline = contentView.findViewById(R.id.tv_landline);
        tvPhone.setOnClickListener(v -> {
            sure(context.getString(R.string.phone_num));
            popupWindow.dismiss();
        });
        tvQq.setOnClickListener(v -> {
            sure(context.getString(R.string.qq));
            popupWindow.dismiss();
        });
        tvWeChat.setOnClickListener(v -> {
            sure(context.getString(R.string.we_chat));
            popupWindow.dismiss();
        });
        tvEmail.setOnClickListener(v -> {
            sure(context.getString(R.string.email));
            popupWindow.dismiss();
        });
        tvLandline.setOnClickListener(v -> {
            sure(context.getString(R.string.landline));
            popupWindow.dismiss();
        });
    }

    public void showPopupWindow(View view) {
        if (!popupWindow.isShowing()) {
            popupWindow.showAsDropDown(view, -30, view.getHeight()-30);
        }
    }

    /**
     * android7.0
     */
    @Override
    public void showAsDropDown(View anchor) {
        if (Build.VERSION.SDK_INT == 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }

    public abstract void sure(String type);
}
